#! /usr/bin/python3
# -*- coding: UTF-8 -*-

if __name__ == "__main__":
	raise Exception("不支持从这里调用")

__version__ = "1.0"
__all__ = ["MouseChshData", "mousechsh_data_merge"]

from com.mousechsh.common.data.MouseChshModel import MouseChshModel


class MouseChshData:
	_data = []

	def get(self, index, key):
		if index < 0 or index >= len(self._data):
			return None
		return self._data[index].get(key, None)

	def set(self, index, key, value):
		if index < 0 or index >= len(self._data):
			return
		self._data[index][key] = value

	def add(self, key, value):
		item = {key: value}
		self.append(item)

	def append(self, item):
		self._data.append(item)


def mousechsh_data_merge(target, diff, *, default=None):
	if target is None or diff is None:
		return target
	if not isinstance(target, MouseChshModel):
		return target
	if not isinstance(diff, MouseChshModel):
		return target
	for item in target.data():
		d = diff.data().get(item, default)
		if d != default and d != target.data().get(item, default):
			target.set(item, d)
	for item in diff.data():
		d = target.data().get(item, default)
		if d == default:
			target.set(item, diff.data().get(item, default))
	return target
